<template>
    <div>
        <el-row class="rowCl">
            <el-col :span="8"><div class="grid-content bg-purple"> &nbsp;</div></el-col>
            <el-col :span="8"><div class="grid-content bg-purple-light">


                <el-card class="box-card">
                    <h1>修改密码</h1>

                    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                        <el-form-item label="输入旧密码" prop="oldPwd">
                            <el-input type="password" v-model="ruleForm.oldPwd" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="pass">
                            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="checkPass">
                            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>

            </div>
            </el-col>
            <el-col :span="8"><div class="grid-content bg-purple">&nbsp;</div></el-col>
        </el-row>

    </div>
</template>

<script>
    export default {
        name: "ChangePwdView",

        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入旧密码'));
                } else {
                    if (this.ruleForm.oldPwd !== '') {
                        this.$refs.ruleForm.validateField('pass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入新密码'));
                } else if (value !== this.ruleForm.checkPass) {
                    callback(new Error('新密码和旧密码不一致!'));
                } else {
                    callback();
                }
            };
            var validatePass3 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入新密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    oldPwd: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass3, trigger: 'blur' }
                    ],
                    oldPwd: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                //userId在session里面，登录用户的id
                this.$axios.get("api/sysuser/updateUserPwd?id="+this.userId+"&newPwd="+this.pass).then(r =>{
                    if (r.data == true) {
                        console.log(formName)
                        alert('修改成功!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>